package com.smec.remes.module.register;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.smec.remes.base.SmecBasePresenter;
import com.smec.remes.bean.AbleMatnr;
import com.smec.remes.bean.NumberPrompt;
import com.smec.remes.bean.SearchContract;
import com.smec.remes.bean.SubmitRegister;
import com.smec.remes.smecManager.SmecRetrofit.ApiException;
import com.smec.remes.smecManager.SmecRetrofit.BaseSubscriber;
import com.smec.remes.smecManager.SmecRetrofit.HttpResult;
import com.smec.remes.util.CommonUtils;

import java.util.ArrayList;
import java.util.HashMap;

import rx.Observable;

import static com.smec.remes.module.register.SmecRegisterFragment.SUBMIT_FAILED;
import static com.smec.remes.module.register.SmecRegisterFragment.SUBMIT_FAILED_M;
import static com.smec.remes.module.register.SmecRegisterFragment.SUBMIT_SUCCESS;

/**
 * Created by Administrator on 2017/12/9.
 */

public class SmecRegisterPresenter  extends SmecBasePresenter {


    public void _Month(String condition,String orgType,int pageNumber){
        HashMap<String,Object> map=new HashMap<>();
        map.put("condition",condition);
        map.put("orgType",orgType);
        map.put("pageSize",5);
        map.put("pageNumber",pageNumber);

        Observable<HttpResult<ArrayList<SearchContract>>> observable=smecPackingService._month(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<ArrayList<SearchContract>>>() {
            @Override
            public void onNext(HttpResult<ArrayList<SearchContract>> arrayListHttpResult) {
                ArrayList<SearchContract> arrayList=arrayListHttpResult.getData();
                mRxbus.post("MAINTAIN",arrayList);
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post("MAINTAINFAIL",e.toString());
            }
        });
    }

    public void searchContract(String condition,String orgType,int pageNumber){
        HashMap<String,Object> map=new HashMap<>();
        map.put("condition",condition);
        map.put("orgType",orgType);
        map.put("pageSize",5);
        map.put("pageNumber",pageNumber);

        Observable<HttpResult<ArrayList<SearchContract>>> observable=smecPackingService.searchContract(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<ArrayList<SearchContract>>>() {
            @Override
            public void onNext(HttpResult<ArrayList<SearchContract>> arrayListHttpResult) {
                ArrayList<SearchContract> arrayList=arrayListHttpResult.getData();
                mRxbus.post("CONTRACT",arrayList);
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post("CONTRACTFAIL",e.toString());
            }
        });
    }

    public void _searchContract(String condition,String orgType,int pageNumber){
        HashMap<String,Object> map=new HashMap<>();
        map.put("condition",condition);
        map.put("orgType",orgType);
        map.put("pageSize",5);
        map.put("pageNumber",pageNumber);

        Observable<HttpResult<ArrayList<SearchContract>>> observable=smecPackingService.searchContract(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<ArrayList<SearchContract>>>() {
            @Override
            public void onNext(HttpResult<ArrayList<SearchContract>> arrayListHttpResult) {
                ArrayList<SearchContract> arrayList=arrayListHttpResult.getData();
                mRxbus.post("_CONTRACT",arrayList);
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post("_CONTRACTFAIL",e.toString());
            }
        });
    }



    public void ableMatnr(String condition, int pageNumber){
        HashMap<String,Object> map=new HashMap<>();
        map.put("condition",condition);
        map.put("type","REGISTER");
        map.put("pageSize",10);
        map.put("pageNumber",pageNumber);

        Observable<HttpResult<ArrayList<AbleMatnr>>> observable=smecPackingService.ableMatnr(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<ArrayList<AbleMatnr>>>() {
            @Override
            public void onNext(HttpResult<ArrayList<AbleMatnr>> arrayListHttpResult) {
                ArrayList<AbleMatnr> arrayList=arrayListHttpResult.getData();
                mRxbus.post("MANTR",arrayList);
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post("MANTRFAIL",e.toString());
            }
        });
    }

    public void submitRegister(SubmitRegister submitRegister){
        Observable<HttpResult> observable=smecPackingService.submitRegister(submitRegister);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult>() {
            @Override
            public void onError(Throwable e) {
                if(e instanceof ApiException){
                    ApiException a=(ApiException)e;
                    mRxbus.post(SUBMIT_FAILED_M,a.getMessage());
                    return;
                }
                mRxbus.post(SUBMIT_FAILED,new Object());
            }

            @Override
            public void onNext(HttpResult httpResult) {
                mRxbus.post(SUBMIT_SUCCESS,new Object());
            }
        });
    }

    public void contractCheck(String eleContractNo){
        HashMap<String,String> hashMap=new HashMap<>();
        hashMap.put("eleContractNo",eleContractNo);
       Observable<HttpResult> observable=smecPackingService._contractCheck(hashMap);
       schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult>() {
           @Override
           public void onNext(HttpResult httpResult) {
               mRxbus.post("contractCheck",httpResult);
           }

           @Override
           public void onError(Throwable e) {
               try{
                   //ee.getMessage()   有时候输出异常会为空
                   ApiException ee=(ApiException)e;
                   String error=ee.getMessage();
                   mRxbus.post("contractCheckFAIL",error);
               }catch (Exception e1){
                   super.onError(e1);
               }

           }
       });
    }

    public void prompt(){
        Observable<HttpResult<ArrayList<NumberPrompt>>> observable=smecPackingService._requestNumber(new Object());
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<ArrayList<NumberPrompt>>>() {
            @Override
            public void onNext(HttpResult<ArrayList<NumberPrompt>> arrayListHttpResult) {
                if(CommonUtils.notEmpty(arrayListHttpResult.getData())){
                    ArrayList<String> arrayList=new ArrayList<>();
                    arrayList.add(String.valueOf(arrayListHttpResult.getData().get(0).getCompletedNum()));
                    arrayList.add(String.valueOf(arrayListHttpResult.getData().get(0).getUnbindingNum()));
                    mRxbus.post("getCompletedNum",arrayList);
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }

    public SmecRegisterPresenter(Context mContext) {
        super(mContext);
    }
}
